/**
 *
 * @Description TODO
 * @Author liuer
 * @CreateTime 2024年04月12日 18:16
 */
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:path/path.dart';
import 'package:restart_app/restart_app.dart';
import 'package:shorebird_code_push/shorebird_code_push.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';

class PatchUpdateDialog extends Dialog {
  final ShorebirdCodePush shorebird;
  final BuildContext pageContext;

  const PatchUpdateDialog(
      {super.key, required this.shorebird, required this.pageContext});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            width: 300.w,
            height: 330.h,
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(16)),
            child: Stack(
              children: <Widget>[
                SizedBox(
                  // width: double.infinity,
                  child: Column(
                    // mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      Gaps.vGap20,
                      ClipOval(
                        child: Image.asset(
                          ImageAssets.iconPatchUpdate,
                          fit: BoxFit.fill,
                          width: 144.w,
                          height: 144.w,
                        ),
                      ),
                      Text(ResKey.upgradeNewParchCanUpgrdade.tr,
                          style: ytTextStyle(
                              fontSize: 18.sp,
                              color: Colors.black,
                              fontWeight: FontWeight.bold,
                              height: 2.5.h)),
                      Container(
                          margin: EdgeInsets.only(left: 48.w, right: 42.w),
                          child: Text(ResKey.upgradeNewPatchUpgradeTip.tr,
                              textAlign: TextAlign.center,
                              style: ytTextStyle(
                                fontSize: 14.sp,
                                color: Colour.otherTextColor,
                                height: 1.9.h,
                              ))),
                      Spacer(),
                      BrnSmallMainButton(
                        title: ResKey.upgradeUpdradeNow.tr,
                        width: 260.w,
                        fontWeight: FontWeight.normal,
                        themeData: BrnButtonConfig(smallButtonHeight: 48.h),
                        onTap: () {
                          goToUpdate();
                        },
                      ),
                      const SizedBox(
                        height: 20,
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
          GestureDetector(
              onTap: () {
                Navigator.pop(context);
              },
              child: Offstage(
                child: Container(
                  margin: EdgeInsets.only(top: 24.h),
                  child: Image.asset(
                    ImageAssets.appUpdateClose,
                    fit: BoxFit.fill,
                    width: 34,
                    height: 34,
                  ),
                ),
              ))
        ],
      ),
    );
  }

  ///去更新
  Future<void> goToUpdate() async {
    Get.back();
    showLoadingDialog();
    Future.delayed(const Duration(seconds: 1), () async {
      await shorebird.downloadUpdateIfAvailable();
      Get.back();
      showRestartDialog();
    });
  }

  showLoadingDialog() {
    BrnDialogManager.showSingleButtonDialog(pageContext,
        barrierDismissible: false,
        label: ResKey.cancel.tr,
        titleWidget: Container(
          padding: EdgeInsets.only(
              left: 135.w, right: 135.w, top: 24.h, bottom: 8.h),
          child: const CircularProgressIndicator(
              strokeWidth: 3, color: Colour.primaryColor),
        ),
        messageWidget: Padding(
          padding: EdgeInsets.only(top: 10.h, bottom: 12.h),
          child: Text(
            '补丁包正在下载中...',
            textAlign: TextAlign.center,
            style: TextStyles.f16.boldStyle,
          ),
        ), onTap: () {
      navigator?.pop();
    });
  }

  showRestartDialog() {
    BrnDialogManager.showConfirmDialog(pageContext,
        cancel: ResKey.cancel.tr,
        confirm: '立即重启',
        message: '补丁包下载完成，是否立即重启应用？', onCancel: () {
      navigator?.pop();
    }, onConfirm: () {
      navigator?.pop();
      Restart.restartApp();
    });
  }
}
